package com.sls.core.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

/**
 * @Description MD5加密处理
 * @author Wangxuegang
 * @date 2012-09-03
 */
public class UtilMD5Encryption {

	private static Logger log = Logger.getLogger(UtilMD5Encryption.class);

	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5Encryption(String data) {
		String dataStr = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(data.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			dataStr = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			log.error(e);
		}

		return dataStr;
	}

	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5EncryptionOfGBK(String data) {
		String dataStr = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(data.getBytes("GBk"));
			} catch (Exception e) {
				log.error(e);
			}

			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			dataStr = buf.toString();

		} catch (NoSuchAlgorithmException en) {
			log.error(en);
		}

		return dataStr;
	}

	/**
	 * MD5加密(生成大写)
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5EncryptionForCapital(String data) {
		String dataStr = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(data.getBytes("gb2312"));
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			dataStr = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			log.error(e);
		} catch (UnsupportedEncodingException e) {
			log.error(e);
		}

		return dataStr.toUpperCase();
	}
	
	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5EncryptionOfGB2312(String data) {
		String dataStr = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(data.getBytes("GB2312"));
			} catch (Exception e) {
				log.error(e);
			}

			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			dataStr = buf.toString();

		} catch (NoSuchAlgorithmException en) {
			log.error(en);
		}

		return dataStr;
	}
	
	/**
	 * MD5加密
	 * 
	 * @param data
	 *            被加密的字符串
	 * @return
	 */
	public static String createMD5EncryptionOfUTF8(String data) {
		String dataStr = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(data.getBytes("UTF-8"));
			} catch (Exception e) {
				log.error(e);
			}

			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i = i + 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			dataStr = buf.toString();

		} catch (NoSuchAlgorithmException en) {
			log.error(en);
		}

		return dataStr;
	}


	
//	public static void main(String[] args){
//		String str = createMD5EncryptionOfGBK("XF06307D1301290522182013012900000179ORDER_SUCCESS20130129172219516CC15F4580B259BBABA8E812AAD75C");
//		System.out.println(str);
//		String str1 = createMD5Encryption("XF06307D1301290522182013012900000179ORDER_SUCCESS20130129172219516CC15F4580B259BBABA8E812AAD75C");
//		System.out.println(str1);
//	}
}
